<?php

namespace App\depository;

use App\Enum\RolesEnum;
use App\Models\SubCodeModel;

class SubCodeDep
{
    public $model;

    public function __construct()
    {
        $this->model = new SubCodeModel();
    }

    public function add($data)
    {
        $res = $this->model->insert($data);
        return $res;
    }

    public function addGetId($data){
        $res = $this->model->insertGetId($data);
        return $res;
    }

    public function upd($id, $data)
    {
        $res = $this->model->where('id', $id)->update($data);
        return $res;
    }

    public function del($id)
    {
        $res = $this->model->where('id', $id)->delete();
        return $res;
    }

    public function list($teamId,$param)
    {
        $res = $this->model
            ->where('team_id',$teamId)
            ->when(!empty($param['status']),function($query) use($param){
                return $query->where('status',$param['status']);
            })
            ->when(!empty($param['code']),function($query) use($param){
                return $query->where('code','like',"%{$param['code']}%");
            })
            ->orderBy('id', 'desc')
            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);
        return $res;
    }

  public function firstByCode($code){
        $res = $this->model->where('code',$code)->first();
        return $res;
  }
}
